package com.example.controller;

import com.example.common.annotation.SysLog;
import com.example.common.utils.ApiResult;
import com.example.model.entity.TUser;
import com.example.model.query.SuppliesRealinfoQuery;
import com.example.model.query.TUserQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;

@RestController
@RequestMapping("/test")
@Slf4j
public class test {

    @SysLog(operDesc = "Test模拟登录",operModul="登录",operType = "用户")
    @PostMapping(value = "/login")
    public ApiResult<Boolean> login(@RequestBody TUser user){
        ApiResult<Boolean> apiResult = new ApiResult<>();
        //用户认证信息
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                user.getUserName(),
                user.getPassword()
        );
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
//            subject.checkRole("admin");
//            subject.checkPermissions("query", "add");
        } catch (UnknownAccountException e) {
            log.error("用户名不存在！", e);
            return  new ApiResult<>(333,"用户名不存在！");
        } catch (AuthenticationException e) {
            log.error("账号或密码错误！", e);
            return  new ApiResult<>(333,"账号或密码错误！");
        } catch (AuthorizationException e) {
            log.error("没有权限！", e);
            return  new ApiResult<>(333,"没有权限！");
        }
        return  new ApiResult<>(Boolean.TRUE);
    }

    @SysLog(operDesc = "Test模拟退出",operModul="退出",operType = "用户")
    @PostMapping(value = "/logout")
    public ApiResult<Boolean> logout(@RequestBody String userName){
        ApiResult<Boolean> apiResult = new ApiResult<>();
        return  new ApiResult<>(Boolean.TRUE);
    }

    @SysLog(operDesc = "Test模块操作",operModul="新增",operType = "用户")
    @GetMapping(value = "/welcome")
    public  String  welcome(){
        return "welcome->应急物资管理系统";
    }

    @GetMapping(value = "/testTime")
    public  Date  testTime(){
        return  new Date();
    }

    @SysLog(operDesc = "当前登录用户信息",operModul="用户信息",operType = "用户")
    @GetMapping(value = "/getUserInfo")
    public  Object  getUserInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TUserQuery user = (TUserQuery)authentication.getPrincipal();
        return  user;
    }

    @PostMapping(value = "/test")
    public  SuppliesRealinfoQuery  testVTime1(){
        SuppliesRealinfoQuery obj = new SuppliesRealinfoQuery();
        obj.setCreateTime(new Date());
        return  obj;
    }
}
